# -*- coding: utf-8 -*-

def generateTagText(tag_list):
    return '\n'.join(map(lambda x: '<category>' + x + '</category>', tag_list))

class EntryGenerator:
    def __init__(self, title = "First item title", link = "http://example.org/item/1", id = "http://example.org/guid/1", description = "some HTML", pub_date = "Thu, 05 Sep 2002 0:00:01 GMT", tags = None):
        # Some default values
        self.title = title
        self.link = link
        self.id = id
        self.description = description
        self.pub_date = pub_date

        if tags is None:
            self.tags = []
        else:
            self.tags = tags

    def createEntryText(self):
        entry_template = """
            <item>
                <title>%s</title>
                <link>%s</link>
                %s
                <description>
                    %s
                </description>
                <author>mark@example.org</author>
                <comments>http://example.org/comments/1</comments>
                <enclosure url="http://example.org/audio/demo.mp3" length="1069871" type="audio/mpeg"/>
                <guid>%s</guid>
                <pubDate>%s</pubDate>
            </item>
            """
        result = entry_template % tuple([self.title, self.link, generateTagText(self.tags), self.description, self.id, self.pub_date])
        return result

class FeedGenerator:
    def __init__(self, title = "Sample Feed", link = "http://example.org/", pub_date = "Thu, 05 Sep 2002 0:00:01 GMT", channel_items = None, tags = None):
        # Some default values to use in tests

        self.title = title
        self.link = link
        self.pub_date = pub_date

        if channel_items is None:
            self.channel_items = [EntryGenerator().createEntryText()]
        else:
            self.channel_items = channel_items
        if tags is None:
            self.tags = []
        else:
            self.tags = tags

        self.default_feed = self.createFeedText()

    def createFeedText(self):
        # TODO: can now create feeds with only one item
        feed_template = """<?xml version="1.0" encoding="utf-8"?>
            <rss version="2.0">
            <channel>
                <title>%s</title>
                <description>For documentation &lt;em&gt;only&lt;/em&gt;</description>
                <link>%s</link>
                <language>en</language>
                <copyright>Copyright 2004, Mark Pilgrim</copyright>
                <managingEditor>editor@example.org</managingEditor>
                <webMaster>webmaster@example.org</webMaster>
                <pubDate>%s</pubDate>
                <generator>Sample Toolkit</generator>
                <docs>http://feedvalidator.org/docs/rss2.html</docs>
                %s
                <cloud domain="rpc.example.com"
                    port="80"
                    path="/RPC2"
                    registerProcedure="pingMe"
                    protocol="soap"/>
                <ttl>60</ttl>
                <image>
                    <url>http://example.org/banner.png</url>
                    <title>Example banner</title>
                    <link>http://example.org/</link>
                    <width>80</width>
                    <height>15</height>
                </image>
                <textInput>
                    <title>Search</title>
                    <description>Search this site:</description>
                    <name>q</name>
                    <link>http://example.org/mt/mt-search.cgi</link>
                </textInput>
                %s
            </channel>
            </rss>
            """
        result = feed_template % (self.title, self.link, generateTagText(self.tags), self.pub_date, '\n'.join(self.channel_items))
        return result

